package com.css.bjdt_report.report.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.report.entity.RptTemplateCondition;
import com.css.bjdt_report.report.entity.RptTemplateCondition.ConditionType;
import com.css.bjdt_report.report.entity.ViewRptTemplateCondition;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.util.PageQuerys;

@Service
public class ReportConditionService {
	@Autowired
	private Dao dao;

	public RptTemplateCondition insert(RptTemplateCondition record) {
		if (record == null) {
			return null;
		}
		if (StringUtils.isBlank(record.getConditionId())) {
			record.setConditionId(CommonUtil.getUUID());
		}
		return dao.insert(record);
	}

	public void insertList(List<RptTemplateCondition> rptTemplateConditionList) {
		for (RptTemplateCondition rptTemplateCondition : rptTemplateConditionList) {
			insert(rptTemplateCondition);
		}
	}

	public boolean batchInsert(List<RptTemplateCondition> records) {
		if (records == null || records.size() == 0) {
			return false;
		}
		for (RptTemplateCondition condition : records) {
			if (StringUtils.isBlank(condition.getConditionId())) {
				condition.setConditionId(CommonUtil.getUUID());
			}
		}
		dao.insert(records);
		return true;
	}

	public RptTemplateCondition getARptTemplateCondition(String recordId) {
		return dao.fetch(RptTemplateCondition.class, Cnd.where("CONDITION_ID", "=", recordId));
	}

	/**
	 * 更新条件
	 * @param record
	 * @return
	 */
	public int update(RptTemplateCondition record) {
		return dao.updateIgnoreNull(record);
	}

	//删除单个查询条件
	public int delete(String recordId) {
		return dao.delete(RptTemplateCondition.class, recordId);
	}

	//删除总控条件、行条件、列条件
	public int deleteTotalRowCol(String templateId) {
		return dao.clear(RptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "in", "1,2,3"));
	}

	//删除总控条件、行条件、列条件
	public int deleteCellRowCol(String templateId, String row, String col) {
		return dao.clear(RptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId)
				.and("CONDITION_TYPE", "=", "4").and("ABS_ROW_NUM", "=", row).and("ABS_COL_NUM", "=", col));
	}

	public int clear(List<String> recordIds) {
		return dao.clear(RptTemplateCondition.class, Cnd.where("CONDITION_ID", "IN", recordIds));
	}

	public List<RptTemplateCondition> getConditionByTemplateId(String templateId) {
		if (StringUtils.isBlank(templateId)) {
			return null;
		}
		return dao.query(RptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId));
	}

	//根据查询的类型，查询出所有的条件
	public List<ViewRptTemplateCondition> getConditionByType(String templateId, ConditionType type) {
		return dao.query(ViewRptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "=", type.getKey()).asc("ORDER_ID"));
	}

	/**
	 * 分页查询条件
	 * @param cnd
	 * @return
	 */
	public Map getConditionByTypeLigerUi(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, ViewRptTemplateCondition.class, cnd, pageNum, pageSize);
		return result;
	}

	//根据行列条件查询出当前的单元格条件
	public List<ViewRptTemplateCondition> getCellConditionByType(String templateId, ConditionType type, String row,
			String col) {
		return dao.query(ViewRptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "=", type.getKey())
						.and("ABS_ROW_NUM", "=", row).and("ABS_COL_NUM", "=", col).asc("ORDER_ID"));
	}

	public Map<String, List<ViewRptTemplateCondition>> buildConditionMapByTemplateId(String templateId) {
		Map<String, List<ViewRptTemplateCondition>> conditionMap = new HashMap<String, List<ViewRptTemplateCondition>>();
		List<ViewRptTemplateCondition> totalConditions = getConditionByType(templateId, ConditionType.total);
		conditionMap.put(ConditionType.total.getKey(), totalConditions);
		List<ViewRptTemplateCondition> rowConditions = getConditionByType(templateId, ConditionType.row);
		conditionMap.put(ConditionType.row.getKey(), rowConditions);
		List<ViewRptTemplateCondition> colConditions = getConditionByType(templateId, ConditionType.col);
		conditionMap.put(ConditionType.col.getKey(), colConditions);
		List<ViewRptTemplateCondition> cellConditions = getConditionByType(templateId, ConditionType.cell);
		conditionMap.put(ConditionType.cell.getKey(), cellConditions);
		return conditionMap;
	}
}
